﻿using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace BuyWithoutLose.Areas.Admin.Controllers
{
    /// <summary>
    /// 产品分类
    /// </summary>
    [Area("admin")]
    public class ProudctCategoryController : BaseAdminController
    {
        private readonly IRepository<ProductType, int> _typePepository;
        public ProudctCategoryController(IRepository<ProductType,int> typePepository, BuyWithoutLoseMoneyDBContext db) : base(db)
        {
            _typePepository = typePepository;
        }
        
        #region 产品分类列表，查询，修改，删除
        /// <summary>
        /// 产品分类的展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var Type=await _typePepository.GetAllListAsync();
            return View(Type);
        }
        /// <summary>
        /// 产品分类的搜索
        /// </summary>
        /// 第一级产品分类名，第二级产品分类名
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(int page, int limit,string firstTypeName,string secondTypeName)
        {
            var type = await _typePepository.GetAllListAsync();
            var query = type.Where(x => true);
            //第一级产品分类名
            if (!string.IsNullOrEmpty(firstTypeName))
            {
                query = query.Where(x => x.FirstTypeName.Contains(firstTypeName));
            }
            //第二级产品分类名
            if (!string.IsNullOrEmpty(secondTypeName))
            {
                query = query.Where(x => x.SecondTypeName.Contains(secondTypeName));
            }
            var count = query.Count();
            var list = query.OrderBy(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList()
               .Select(x => new
               {
                   id = x.Id,
                   firstTypeName= x.FirstTypeName,
                   secondTypeName= x.SecondTypeName,
                   thirdTypeName=x.ThirdTypeName,


               }) ;
            return Json(new { Code = 0, Msg = "", Data = list, Count = count });
        }
        /// <summary>
        /// 产品分类的添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Create()
        {
            var type = await _typePepository.GetAllListAsync();
            ViewBag.Type = type;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(ProductType productType)
        {
            if (ModelState.IsValid)
            {
                await _typePepository.InsertAsync(productType);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，请重试！" });
        }
        /// <summary>
        /// 产品分类的修改
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var type = await _typePepository.GetAllListAsync();
            var t = type.FirstOrDefault(x => x.Id == id);
            return View(t);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(ProductType productType)
        {
            if (ModelState.IsValid)
            {
                await _typePepository.UpdateAsync(productType);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
        /// <summary>
        /// 删除某个分类
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Del(int id)
        {
            ProductType p = new ProductType() { Id = id };
            if (ModelState.IsValid)
            {
                await _typePepository.DeleteAsync(p);
                return Json(new { isSuccess = true, msg = "删除成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "删除失败，请重试！" });
        }
        #endregion
    }
}
